# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pyside6
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-shiboken6
         ${MINGW_PACKAGE_PREFIX}-${_realname}
         ${MINGW_PACKAGE_PREFIX}-${_realname}-tools)
pkgver=6.5.2
pkgrel=2
pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
url="https://doc.qt.io/qtforpython/"
license=("spdx:LGPL-2.1-or-later")
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-3d"
            "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
            "${MINGW_PACKAGE_PREFIX}-qt6-charts"
            "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
            "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
            "${MINGW_PACKAGE_PREFIX}-qt6-location"
            "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
            "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
            "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
            "${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
            "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
            "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
            "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
            "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
            "${MINGW_PACKAGE_PREFIX}-qt6-speech"
            "${MINGW_PACKAGE_PREFIX}-qt6-svg"
            "${MINGW_PACKAGE_PREFIX}-qt6-tools"
            "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
            "${MINGW_PACKAGE_PREFIX}-qt6-websockets")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-packaging"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-qt6-3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts"
             "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-location"
             "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
             "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             "${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
             "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt6-speech"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
_pkgfn=pyside-setup-everywhere-src-$pkgver
source=(https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-$pkgver-src/${_pkgfn}.tar.xz
        001-fix-building-on-mingw.patch)
sha256sums=('90dbf1d14fcd41c98a7cbea44b8a4951e10d0b798e154749756e4946654d1ba8'
            '5f71b618e0ad687e9282fa4b8c3815beaf69b78666516bfce57cb039822f4ac6')

prepare() {
  cd "${srcdir}"/${_pkgfn}
  patch -p1 -i "${srcdir}"/001-fix-building-on-mingw.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTS=OFF \
    -DFORCE_LIMITED_API=OFF \
    -DNO_QT_TOOLS="yes" \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_pkgfn}

  PYTHONPATH="${srcdir}"/build-${MSYSTEM}/sources \
  ${MINGW_PREFIX}/bin/cmake --build .
}

package_shiboken6() {
  pkgdesc="Generates bindings for C++ libraries using CPython source code (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-clang"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-libxslt")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/shiboken6

  install -Dm644 ${_pkgfn}/sources/shiboken6/COPYING "$pkgdir"${MINGW_PREFIX}/share/licenses/shiboken6/LICENSE

  # Fix *.pc files
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/shiboken6.pc

  # Fix *.cmake files
  for _f in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/Shiboken6/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Install egg-info
  cd "${srcdir}"/${_pkgfn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken6
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken6-generator
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken6*.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}

package_pyside6() {
  pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-shiboken6"
           "${MINGW_PACKAGE_PREFIX}-qt6-declarative")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside6

  install -Dm644 ${_pkgfn}/sources/pyside6/COPYING "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  # Install egg-info
  cd "${srcdir}"/${_pkgfn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=pyside6
  cp -r PySide6.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
  # Install pyi files
  install -Dm644 "${srcdir}"/build-${MSYSTEM}/sources/pyside6/PySide6/*.pyi -t "$pkgdir"/$(cygpath ${_pythonpath})/PySide6
}

package_pyside6-tools() {
  pkgdesc="Tools for PySide6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-pyside6")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside-tools
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
